package com.netease.operator.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.netease.entity.Entity;
import com.netease.entity.impl.User;
import com.netease.operator.Operator;

public class UserOperator implements Operator {

	public static Map<Integer, Entity> users = new HashMap<Integer, Entity>();
	
	@Override
	public Entity get(int id) {
		Set<Integer> key = users.keySet();
		if(key.contains(id)) {
			return users.get(id);
		}
		return null;
	}
	
	@Override
	public String getNameById(int id) {
		User user = (User)get(id);
		return user.getName();
	}
	
	@Override
	public boolean add(Entity entity) {
		boolean result = false;
		if(users != null && !users.keySet().contains(entity.getId())) {
			users.put(entity.getId(), entity);
			result = true;
		}
		return result;
	}
	
	@Override
	public boolean update(Entity entity) {
		boolean result = false;
		if(users != null && users.keySet().contains(entity.getId())) {
			users.remove(entity.getId());
			users.put(entity.getId(), entity);
			result = true;
		}
		return result;
	}
	
}
